//
//  StringUtils.m
//  SKStock
//
//  Created by thPark on 11. 1. 3..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "StringUtils.h"

NSString* strXX1Type[] = {
	@"S7",
	@"D7",
	@"S8",
	@"D8",
	@"11",
	@"E1",
	@"12",
	@"E2",
	@"C1",
	@"E3",
	@"C2",
	@"E4",
	@"21",
	@"E5",
	@"22",
	@"E6",
	@"13",
	@"E5",
	@"14",
	@"E6",
	@"15",
	@"H1",
	@"16",
	@"H2",
	@"D1",
	@"I1",
	@"D2",
	@"I2",
	@"L1",
	@"L2",
	@"L3",
};

NSString* strXX1Name[] = {
	@"매수정정",
	@"매도정정",
	@"매수취소",
	@"매도취소",
	@"장내매도",
	@"단일가장내매도",
	@"장내매수", 
	@"단일가장내매수",
	@"신용매도",
	@"단일가장외매도",
	@"신용매수",
	@"단일가장외매수",
	@"채권매도",
	@"단일가신용매도",
	@"채권매수", 
	@"단일가신용매수",
	@"코스닥매도",
	@"단일가신용매도",
	@"코스닥매수", 
	@"단일가신용매수",
	@"OTCBB매도",
	@"유통대주매도",
	@"OTCBB매수",
	@"유통대주매수상환",
	@"단주매도",
	@"자기대주매도",
	@"단주매수",
	@"자기대주매수상환",
	@"담보대출상환",
	@"담보대출정정",
	@"담보대출취소",
};

@implementation StringUtils

+ (NSString *)toCurrencyFormatWithIntValue:(int)value {
	NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
	[formatter setNumberStyle:kCFNumberFormatterDecimalStyle];
	NSString * ret = [formatter stringFromNumber:[NSNumber numberWithInt:value]];
	[formatter release];
	return ret;
}

+ (NSString *)toCurrencyFormatWithStrValue:(NSString *)value {
	int intValue = [value intValue];
	return [StringUtils toCurrencyFormatWithIntValue:intValue];
}

+ (NSString *)toCurrencyFormatWithULongLongValue:(uint64_t)value {
	NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
	[formatter setNumberStyle:kCFNumberFormatterDecimalStyle];
	NSString * ret = [formatter stringFromNumber:[NSNumber numberWithUnsignedLongLong:value]];
	[formatter release];
	return ret;
}

+ (NSString *)toCurrencyFormatWithDoubleValue:(double)value {
	NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
	[formatter setNumberStyle:kCFNumberFormatterDecimalStyle];
	NSString * ret = [formatter stringFromNumber:[NSNumber numberWithDouble:value]];
	[formatter release];
	return ret;
}

+ (BOOL)isCheckNotNull:(NSString*)str{
	if(str == nil){
		return NO;
	}else if([str length] <= 0){
		return NO;
	}
	return YES;	
}

+ (CGSize)getStringSize:(NSString*)str fsize:(int)fontSize{
	CGSize maxSize = CGSizeMake(260, 1024);
	CGSize textSize = [str sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap];
	return textSize;
}

+ (BOOL)isContainStr:(NSString*)str chekStr:(NSString*)chekStr{
	NSRange range = [str rangeOfString:chekStr];
	if(range.location != NSNotFound){
		return YES;
	}else{
		return NO;
	}
}

+ (NSString*)getPassWordString:(NSString*)str{
	int len = [str length];
	if(len <= 0){
		return @"";
	}else{
		NSMutableString* strPass = [[NSMutableString alloc] init];
		for(int a=0; a<len; a++){
			[strPass appendString:@"*"];
		}
		return [strPass autorelease];
	}
}

+ (NSString *)clearDateStr:(NSString*)str chekStr:(NSString*)checkStr replaceStr:(NSString*)replaceStr{
	return [str stringByReplacingOccurrencesOfString:checkStr withString:replaceStr];
}

+ (NSString*)compareStrSign:(NSString*)aBase aMoney:(NSString*)aMoney{
	double baseMoney = [[StringUtils clearDateStr:aBase chekStr:@"," replaceStr:@""] doubleValue];
	double checkMoney = [[StringUtils clearDateStr:aMoney chekStr:@"," replaceStr:@""] doubleValue];
	
	//LOG_TRACE(@"------ %d / %d", baseMoney, checkMoney);
	if(checkMoney == 0){
		return @"-";
	}else if(baseMoney > checkMoney){
		return @"▼";
	}else if(baseMoney < checkMoney){
		return @"▲";
	}else{
		return @"-";
	}
}

+ (NSString*)changeNumToStr:(NSString*)str{
	if(![StringUtils isCheckNotNull:str]){
		return @"0";
	}
	
	if([StringUtils isContainStr:str chekStr:@"."]){
		NSArray* arrFS = [str componentsSeparatedByString:@"."];
		
		int money = [[StringUtils clearDateStr:[arrFS objectAtIndex:0] chekStr:@"," replaceStr:@""] intValue];
		//LOG_TRACE(@"--------- %f", money);
		NSNumberFormatter *fmt = [[[NSNumberFormatter alloc] init]autorelease];
		[fmt setNumberStyle:NSNumberFormatterDecimalStyle];
		return [NSString stringWithFormat:@"%@.%@", [fmt stringFromNumber:[NSNumber numberWithInt:money]], [arrFS objectAtIndex:1]];
	}else{
		int money = [[StringUtils clearDateStr:str chekStr:@"," replaceStr:@""] intValue];
		NSNumberFormatter *fmt = [[[NSNumberFormatter alloc] init]autorelease];
		[fmt setNumberStyle:NSNumberFormatterDecimalStyle];
		return [fmt stringFromNumber:[NSNumber numberWithInt:money]];
		
	}
}

+ (NSString*)changeNumToStrWithoutWon:(NSString*)str{
    if(![StringUtils isCheckNotNull:str]){
		return @"0";
	}
    
    str = [StringUtils clearDateStr:str chekStr:@"원" replaceStr:@""];
    str = [StringUtils clearDateStr:str chekStr:@" " replaceStr:@""];
	
	if([StringUtils isContainStr:str chekStr:@"."]){
		NSArray* arrFS = [str componentsSeparatedByString:@"."];
		
		int money = [[StringUtils clearDateStr:[arrFS objectAtIndex:0] chekStr:@"," replaceStr:@""] intValue];
		//LOG_TRACE(@"--------- %f", money);
		return [NSString stringWithFormat:@"%d.%@",money, [arrFS objectAtIndex:1]];
	}else{
		int money = [[StringUtils clearDateStr:str chekStr:@"," replaceStr:@""] intValue];
		return [NSString stringWithFormat:@"%d", money];
	}
}

+ (NSString*)changeDoubleNumToStr:(NSString*)str{
	if(![StringUtils isCheckNotNull:str]){
		return @"0";
	}
	
	NSString* temp = [str substringWithRange:NSMakeRange(str.length - 3, 3)];
	double money = [[StringUtils clearDateStr:str chekStr:@"," replaceStr:@""] doubleValue];
    NSNumberFormatter *fmt = [[[NSNumberFormatter alloc] init]autorelease];
    [fmt setNumberStyle:NSNumberFormatterDecimalStyle];
    NSString* result = [fmt stringFromNumber:[NSNumber numberWithInt:money]];
	
    return [result stringByAppendingString:temp];
}

+ (NSString*)getCheckSign:(NSString*)str{
	if([str isEqualToString:@"0"]){
		return @"-";
	}else if([str isEqualToString:@"0.00%"]){
		return @"-";
	}else if([StringUtils isContainStr:str chekStr:@"-"]){
		return @"▼";
	}else if([StringUtils isContainStr:str chekStr:@"/"]){
		return @"▼";
	}else if([str isEqualToString:@"0.0000"]){
		return @"-";
    }else if([str isEqualToString:@"-0.0000"]){
		return @"-";
    }
	/*
	else if([StringUtils isContainStr:str chekStr:@"+"]){
		return @"▲";
	}
	*/
	else{
		return @"▲";
	}
}

+ (NSString*)getDeleteSign:(NSString*)str{
	NSString* changeStr = [StringUtils clearDateStr:str chekStr:@"+" replaceStr:@""];
	return [StringUtils clearDateStr:changeStr chekStr:@"-" replaceStr:@""];
}

+ (NSString*)getChangeSign:(NSString*)str{
	if([str isEqualToString: @"2"]){
		return @"▲";
	}else if([str isEqualToString: @"1"]){
		return @"↑";
	}else if([str isEqualToString: @"5"]){
		return @"▼";
	}else if([str isEqualToString: @"4"]){
		return @"↓";
	}else{
		return @"-";
	}
}

+ (NSString*)getChangePercent:(NSString*)str{
	return [NSString stringWithFormat:@"%@%%", str];
}

+ (NSString*)moneyFormat:(NSString*)strNumber{
	if([StringUtils isCheckNotNull:strNumber]){
		strNumber = [strNumber stringByReplacingOccurrencesOfString:@"," withString:@""];
		int len = [strNumber length];
		NSMutableString* str = [[NSMutableString alloc] init];
		int startPosition = (len%3);
		
		if(startPosition != 0){
//			LOG_TRACE(@"======== %d / %@", startPosition, [strNumber substringToIndex:startPosition]);
			[str appendString:[strNumber substringToIndex:startPosition]];
			if(len > 3){
				[str appendString:@","];
			}
		}
		for(int a=startPosition; a<len; a++){
			[str appendString:[NSString stringWithFormat:@"%c", [strNumber characterAtIndex:a]]];
			if(a!=(len-1)){
				if((a-startPosition)%3 == 2){
					[str appendString:@","];
				}
				
			}
		}
		return [str autorelease];
	}else{
		return @"";
	}
	
	
	/*
    //스트링을 INT로 변경
	strNumber = [strNumber stringByReplacingOccurrencesOfString:@"," withString:@""];
    int nTmp = [strNumber intValue];
    
    NSNumberFormatter *fmt = [[[NSNumberFormatter alloc] init]autorelease];
    [fmt setNumberStyle:NSNumberFormatterDecimalStyle];
    
	if(nTmp > 0){
		NSString *formatedString = [fmt stringFromNumber:[NSNumber numberWithInt:nTmp]];
		return formatedString;
	}else{
		return @"";
	}
	*/
}

+ (NSString*)getStrXX1Type:(NSString*)type{
	for(int a=0; a<31; a++){
		if([strXX1Type[a] isEqualToString:type]){
			return strXX1Name[a];
		}
	}
	return @"";
}

+(BOOL)isNumberStringOnly:(NSString*)str{
	if(![self isCheckNotNull:str]){
		return NO;
	}
	int len = [str length];
	for(int a=0; a<len; a++){
		char temp = [str characterAtIndex:a];
		if(temp == ',' || (temp >= '0' && temp <= '9')){
			
		}else{
			return NO;
		}
	}
	
	return YES;
}

@end
